package top.tenebrous.termbor.rpc.spi.factory;

import top.tenebrous.termbor.rpc.spi.SPI;
import top.tenebrous.termbor.rpc.spi.SPIClass;
import top.tenebrous.termbor.rpc.spi.loader.ExtensionLoader;

import java.util.Optional;

/**
 * @program: termbor-rpc
 * @description:
 * @author: yazhang6
 * @create: 2024-05-24 09:05
 **/
@SPIClass
public class SpiExtensionFactory implements ExtensionFactory{

    @Override
    public <T> T getExtension(String key, Class<T> clazz) {
        return Optional.ofNullable(clazz)
            .filter(Class::isInterface)
            .filter(cls -> cls.isAnnotationPresent(SPI.class))
            .map(ExtensionLoader::getExtensionLoader)
            .map(ExtensionLoader::getDefaultSpiClassInstance)
            .orElse(null);
    }

}
